# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
猫眼-评论 App 
http://m.maoyan.com/mmdb/comments/movie/1211270.json?_v_=yes&offset=0&startTime=2020-01-08%2018:18:53
根据时间倒序
"""


class MaoYan_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
        }


    def get_comments(self, id , start_time):
        id = id
        start_time = start_time
        # url = f"http://m.maoyan.com/mmdb/comments/movie/1211270.json?_v_=yes&offset=0&startTime=2020-01-08%2018:18:53"
        url = f"http://m.maoyan.com/mmdb/comments/movie/{id}.json?_v_=yes&offset=0&startTime={start_time}"
        time.sleep(1)
        html = HttpUtils.do_request("GET", url, self.headers, "")
        data_json = json.loads(html.text)
        for d in data_json['cmts']:
            dict_data = dict()
            dict_data['nickName'] = d['nickName']  # 昵称
            dict_data['cityName'] = d['cityName']  # 城市
            dict_data['content'] = d['content']  # 评论内容
            dict_data['startTime'] = d['startTime']  # 时间
            dict_data['score'] = d['score']  # 评分
            end_time = d['startTime']
            print(str(dict_data))
        self.get_comments(id, d['startTime'])


if __name__ == '__main__':
    maoyan_get = MaoYan_Get()

    maoyan_get.get_comments("1211270", "2020-01-09 20:00:00")